// test/pages/searchList/searchList.js
let col1H = 0;
let col2H = 0;
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],
    col1: [],
    col2: [],
    isLink:false,
    showLoad:true,
    isempty:false,
    page:1
  },
  // 获取图片
  loadImages(){
    const that = this
    let token = wx.getStorageSync('token')
    let page = that.data.page
    var images = []
    if(that.data.isLink){
      wx.request({
        url: app.globalData.host+'Competition/my_photo_list',
        data:{token,page},
        method:'POST',
        success(res){
          images = res.data.data
          if(images==''){
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            })
            that.setData({
              showLoad:false,
              isempty:true
            })
            return
          }else{
            page+=1
            that.setData({
              images,
              page
            })
          }
        },
        fail(err){
          console.log(err)
        }
      })
    }else{
      var images = JSON.parse(wx.getStorageSync('imgObj'))
    }
    setTimeout(() => {
      let baseId = "img-" + (+new Date());
      for (let i = 0; i < images.length; i++) {
        images[i].id = baseId + "-" + i + '-' + images[i].id;
      }
      that.setData({
          loadingCount: images.length,
          images: images
      });
    }, 500);
  },
  // 图片加载完毕后处理图片
  onImageLoad: function (e) {
    const  that = this
    that.setData({
      showLoad:false
    })
    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width;         //图片原始宽度
    let oImgH = e.detail.height;        //图片原始高度
    let imgWidth = that.data.imgWidth;  //图片设置的宽度
    let scale = imgWidth / oImgW;        //比例计算
    let imgHeight = oImgH * scale;      //自适应高度

    let images = that.data.images;
    let imageObj = null;

    for (let i = 0; i < images.length; i++) {
        let img = images[i];
        if (img.id === imageId) {
            imageObj = img;
            break;
        }
    }

    imageObj.height = imgHeight;

    let loadingCount = that.data.loadingCount - 1;
    let col1 = that.data.col1;
    let col2 = that.data.col2;

    if(col1 == ''){
      col1H += imgHeight;
      col1.push(imageObj);
    }else{
      if (col1H <= col2H) {
        col1H += imgHeight;
        col1.push(imageObj);
      } else {
        col2H += imgHeight;
        col2.push(imageObj);
      }
    }

    let data = {
        loadingCount: loadingCount,
        col1: col1,
        col2: col2
    };

    if (!loadingCount) {
        data.images = [];
    }

    that.setData(data);
  },
  // 查看图片详情
  goDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/downloadPic/downloadPic?id='+id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    if(options.isLink){
      that.setData({
        isLink:true
      })
    }else{
      that.setData({
        isLink:false
      })
    }
    wx.getSystemInfo({
      success: (res) => {
          let ww = res.windowWidth;
          let wh = res.windowHeight;
          let imgWidth = ww * 0.48;
          let scrollH = wh;
          that.setData({
              scrollH: scrollH,
              imgWidth: imgWidth
          });
          that.loadImages();
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})